//
//  B_ActivityDetailVC.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/12.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ActivityDetailVC.h"
#import "B_RightTopAlertView.h"
#import "B_ActivityThreeButtonView.h"
#import "B_ReportListVC.h"
#import "B_ActivityDetailHeaderView.h"
#import "B_ActivityDetailModel.h"
#import "B_ActivityDetailTVC.h"
#import "B_AddGoodsLabelTVC.h"
#import "B_ActivityDetailFooterView.h"
#import "B_TopicDetailHeaderTwo.h"
#import "B_ActivityDetailFooterViewTwo.h"
#import "B_UserMainPageVC.h"
#import "B_CommentListVC.h"
#import "B_ActivityAignUpVC.h"

@interface B_ActivityDetailVC ()

@property (nonatomic, strong) BaseView * mainView;

@property (nonatomic, strong) B_ActivityThreeButtonView * threeButton;

@property (nonatomic, strong) B_ActivityDetailModel * detailModel;

@end

@implementation B_ActivityDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigation];
    self.view = self.mainView;
    [self.mainView addSubview:self.threeButton];
    CGFloat bottom = SCREEN_HEIGHT > 800 ? (49 + 24) : 49;
    [self.threeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mainView.mas_bottom);
        make.left.equalTo(self.mainView.mas_left);
        make.right.equalTo(self.mainView.mas_right);
        make.height.equalTo(@(bottom));
    }];
    [self.mainView setTableViewFrameWithTop:-getNavHeight() left:0 right:0 bottom:-bottom];
    [self getData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavigationBarBackgroundWithImage:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    [self setNavigationBarShadowImge:[UIImage getImageWithColor:[UIColor clearColor] alp:0]];
    
}

- (void) setNavigation {
    WeakSelf(weSelf, self);
    [self setRightImageBarButtonItemWithFrame:CGRectZero image:@"B_topic_details_more" selectImage:@"" ishidden:YES action:^(GUIButton *button) {
        //TODO:执行弹窗
        B_RightTopAlertView * alertView = [B_RightTopAlertView initB_RightTopAlertView];
        [alertView show];
        alertView.dataArray = @[@{@"title" :@"分享", @"image" : @"B_topic_details_bounced_share"},@{@"title" : @"举报", @"image" : @"B_topic_details_bounced_report"}];
        alertView.selectIndex = ^(NSIndexPath *indexPath) {
            if (indexPath.row == 0) {
                //TODO: 执行分享
                HXLog(@"执行分享");
            } else if (indexPath.row == 1) {
                //TODO: 执行举报
                HXLog(@"执行举报");
                B_ReportListVC * vc = [[B_ReportListVC alloc] init];
                vc.type = B_ReportListVCTypeActivity;
                vc.activityId = weSelf.activityId;
                [weSelf pushNextViewController:vc];
            }
        };
        
    }];
}

- (BaseView *)mainView {
    if (!_mainView) {
        _mainView = [[BaseView alloc] init];
        WeakSelf(weSelf, self);
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_ActivityDetailTVC" bundle:nil] forCellReuseIdentifier:@"B_ActivityDetailTVC"];
        [_mainView.tableView registerNib:[UINib nibWithNibName:@"B_AddGoodsLabelTVC" bundle:nil] forCellReuseIdentifier:@"B_AddGoodsLabelTVC"];
        //TODO:有几组
        _mainView.numberOfSectionsBlock = ^NSInteger(UITableView *tableView) {
            return 2;
        };
        
        _mainView.numberOfRowsBlock = ^NSInteger(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                return 4;
            } else if (section == 1) {
                return 1;
            }
            return 0;
        };
        _mainView.viewForHeaderBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_ActivityDetailHeaderView * header = [B_ActivityDetailHeaderView initB_ActivityDetailHeaderView];
                [header.B_imageView sd_setImageWithURL:[NSURL URLWithString:weSelf.detailModel.activityCover]];
                header.B_title.text = weSelf.detailModel.activityTitle;
                return header;
            } else if (section == 1) {
                B_TopicDetailHeaderTwo * header = [B_TopicDetailHeaderTwo initB_TopicDetailHeaderTwo];
                header.B_titleLabel.text = @"详情";
                header.B_titleLabel.font = [UIFont systemFontOfSize:14];
                header.backgroundColor = [UIColor color_content_background_color];
                header.B_button.hidden = YES;
                return header;
            }
            return nil;
        };
        
        _mainView.viewForFooterBlock = ^UIView *(UITableView *tableView, NSInteger section) {
            if (section == 0) {
                B_ActivityDetailFooterView * footer = [B_ActivityDetailFooterView initB_ActivityDetailFooterView];
                [footer.B_UserImageView sd_setImageWithURL:[NSURL URLWithString:weSelf.detailModel.portrait]];
                [footer.B_userTypeImageView setUsetType:weSelf.detailModel.userType];
                footer.B_userName.text = weSelf.detailModel.nickName;
                footer.B_userSign.text = weSelf.detailModel.signature;
                if ([weSelf.detailModel.isFollow isEqualToString:@"1"]) {
                    footer.B_attentionButton.selected = YES;
                } else {
                    footer.B_attentionButton.selected = NO;
                }
                
                return footer;
            } else if (section == 1) {
                B_ActivityDetailFooterViewTwo * footer = [B_ActivityDetailFooterViewTwo initB_ActivityDetailFooterViewTwo];
                footer.imageArray = weSelf.detailModel.imageArray;
                return footer;
            }
            return nil;
        };
        
        _mainView.cellForRowBlock = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath) {
            if (indexPath.section == 0) {
                B_ActivityDetailTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_ActivityDetailTVC" forIndexPath:indexPath];
                
                if (indexPath.row == 0) {
                    cell.B_imageView.image = [UIImage imageNamed:@"B_activity_details_time"];
                    cell.B_title.text = [NSString stringWithFormat:@"%@-%@", [NSObject getTime:weSelf.detailModel.startTime], [NSObject getTime:weSelf.detailModel.overTime]];
                } else if (indexPath.row == 1) {
                    cell.B_imageView.image = [UIImage imageNamed:@"personal_center_my_activities_time_icon@2x"];
                    cell.B_title.text = [NSString stringWithFormat:@"%@元/人", weSelf.detailModel.applyPrice];
                } else if (indexPath.row == 2) {
                    cell.B_imageView.image = [UIImage imageNamed:@"B_activity_home_address"];
                    cell.B_title.text = [NSString stringWithFormat:@"%@%@%@%@   %@", weSelf.detailModel.province, weSelf.detailModel.city, weSelf.detailModel.district, weSelf.detailModel.location, weSelf.detailModel.juli];
                } else if (indexPath.row == 3) {
                    cell.B_imageView.image = [UIImage imageNamed:@"B_activity_details_phone"];
                    cell.B_title.text = [NSString stringWithFormat:@"咨询热线:%@", weSelf.detailModel.applyPhone];
                }
                
                
                return cell;
            } else {
                B_AddGoodsLabelTVC * cell = [tableView dequeueReusableCellWithIdentifier:@"B_AddGoodsLabelTVC" forIndexPath:indexPath];
                cell.B_label.text = weSelf.detailModel.activityInfo;
                return cell;
            }
        };
        
        
        
    }
    return _mainView;
    
}

- (B_ActivityThreeButtonView *)threeButton {
    if (!_threeButton) {
        WeakSelf(weSelf, self);
        _threeButton = [B_ActivityThreeButtonView initB_ActivityThreeButtonView];
        _threeButton.B_ActivityFooterAction = ^(NSInteger index) {
            HXLog(@"index is %ld", index);
            if (index == 0) {       //TODO:评论列表
                B_CommentListVC * vc = [[B_CommentListVC alloc] init];
                vc.topicId = weSelf.detailModel.id;
                [weSelf pushNextViewController:vc];
            } else if (index == 1) {    // TODO:主办方
                B_UserMainPageVC * vc = [[B_UserMainPageVC alloc] init];
                vc.B_userId = weSelf.detailModel.userId;
                [weSelf pushNextViewController:vc];
                
            } else if (index == 2) {    // TODO:参加活动
                
                B_ActivityAignUpVC * vc = [[B_ActivityAignUpVC alloc] init];
                vc.detailModel = weSelf.detailModel;
                
                
                [weSelf pushNextViewController:vc];
                
            }
        };
    }
    return _threeButton;
}

- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/activity/activityInfo?activityId=%@&userId=%@&longitude=%f&latitude=%f", self.activityId, getUserId(), getLocation().coordinate.longitude, getLocation().coordinate.latitude] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.detailModel = [B_ActivityDetailModel mj_objectWithKeyValues:data[@"values"]];
            weSelf.detailModel.imageArray = [NSMutableArray array];
            for (NSDictionary * dic in weSelf.detailModel.activityPictureList) {
                [weSelf.detailModel.imageArray addObject:dic[@"activityPictures"]];
            }
            [weSelf.threeButton.B_leftButton setTitle:[NSString stringWithFormat:@"评论(%@)", safeValue1(weSelf.detailModel.commentCount)] forState:UIControlStateNormal];
            [weSelf.threeButton.B_centerImageView sd_setImageWithURL:[NSURL URLWithString:weSelf.detailModel.portrait]];
            [weSelf.mainView.tableView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
